package com.qxczh.rest.utils;


import io.minio.*;
import io.minio.messages.Bucket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.List;

/**
 * TODO
 *
 * @author Gaosl
 * @className MinioUtil
 * @date 2020/3/17 10:14
 */
@Slf4j
@Component
public class MinioUtils {
    @Resource
    private MinioClient minioClient;

    public void addBucket(String bucketName) throws Exception{
        if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
            System.out.println(bucketName);
        }
    }
    /**
     *
     * @Description 获取minio所有的桶
     * @return java.util.List<io.minio.messages.Bucket>
     **/
    public List<Bucket> getAllBucket() throws Exception {
        // 获取minio中所以的bucket
        List<Bucket> buckets = minioClient.listBuckets();
        for (Bucket bucket : buckets) {
            log.info("bucket 名称:  {}      bucket 创建时间: {}", bucket.name(), bucket.creationDate());
        }
        return buckets;
    }


    /**
     *
     * @Description  将图片上传到minio服务器
     * @param inputStream: 输入流
     * @param objectName: 对象名称
     * @return void
     **/
    public String uploadToMinio(InputStream inputStream,String bucketName, String objectName) {
        try {
            long size = inputStream.available();
            PutObjectArgs putObjectArgs = PutObjectArgs.builder()
                    .bucket(bucketName)
                    .object(objectName)
                    .stream(inputStream, size, -1)
                    .build();
            // 上传到minio
            minioClient.putObject(putObjectArgs);
            inputStream.close();
            return "http://47.119.163.105:9000/"+bucketName+"/"+objectName;
        } catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
            return null;
        }
    }




    /**
     *
     * @Description 根据objectName从minio中下载文件到指定的目录
     * @param objectName: objectName
     * @param fileName: 文件名称
     * @param dir: 文件目录
     * @return void
     **/
    public void downloadImageFromMinioToFile(String bucketName,String objectName, String dir, String fileName) throws Exception {
        GetObjectArgs objectArgs = GetObjectArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .build();
        File file = new File(dir);
        if (!file.exists()) {
            file.mkdirs();
        }
        InputStream inputStream = minioClient.getObject(objectArgs);
        FileOutputStream outputStream =  new FileOutputStream(new File(dir, fileName.substring(fileName.lastIndexOf("/")+1)));
        int length;
        byte[] buffer = new byte[1024];
        while ((length = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, length);
        }
        outputStream.close();
        inputStream.close();
    }

}
